package com.chenzhou.controller;

import com.chenzhou.entity.ProductEntity;
import com.chenzhou.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService service;

    @RequestMapping("/query_product")
    public String seach_product(HttpServletRequest request){
        List<ProductEntity> list = service.queryAll();
        request.setAttribute("plist",list);
        return "product/show_product";
    }

    @RequestMapping("/query_productByID")
    public String seach_productByID(HttpServletRequest request,Integer pid){
        ProductEntity p = service.queryByID(pid);
        request.setAttribute("p",p);
        return "product/modify_product";
    }

    @RequestMapping("/add_product")
    public String add_Product(Model model,ProductEntity productEntity){
        int a = service.add_product(productEntity);
        if(a>0) {
            return "redirect:/query_product";
        }else {
            return "forward:/error.jsp";
        }
    }

    @RequestMapping("/modify_product")
    public String modify_Product(Model model,ProductEntity productEntity){
        int a = service.modify_product(productEntity);
        if(a>0) {
            return "redirect:/query_product";
        }else {
            return "forward:/error.jsp";
        }
    }

    @RequestMapping("/delete_product")
    public String delete_Product(Model model,Integer pid){
        int a = service.delete_product(pid);
        if(a>0) {
            return "redirect:/query_product";
        }else {
            return "forward:/error.jsp";
        }
    }
}
